//
//  RBGetCouponListVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/30.
//

#import "RBGetCouponListVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import "RBSearchTagView.h"
#import "RBThemeConfig.h"
#import <Masonry.h>
#import "RBNavigationBar.h"
#import "RBTextField.h"
#import "UIButton+RBAdd.h"
#import <UIColor+YYAdd.h>
#import "RanTool.h"
#import "RBGetCouponListCell.h"
#import "UIViewController+RBAdd.h"
#import "RBGetCouponResponse.h"
#import "RBRefreshHeader.h"
#import "RBRefreshFooter.h"
#import "RBCouponPayVC.h"
#import "RBCouponCreateOrderResponse.h"

@interface RBGetCouponListVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSource;

@property(nonatomic, strong) RBGetCouponResponse *response;

@end

@implementation RBGetCouponListVC

#pragma mark - Lifecycle

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        self.ignoreStatusAjust = YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self.listView.mj_header beginRefreshing];
}

#pragma mark - Private

- (UIView *)rbDesScrollView
{
    return self.collectionView;
}

- (void)setup
{
    self.view.backgroundColor = UIColor.whiteColor;
}

- (void)fillData
{
    [self.dataSource removeAllObjects];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.sectionInset = UIEdgeInsetsMake(Zoom(17), 0, Zoom(17), 0);
        group.minimumLineSpacing = Zoom(10);
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
        group;
    })];
    [self appendData];
    
}

- (void)appendData
{
    __weak typeof(self) weak_self = self;
    NSMutableArray *arrayM = ({
        RBGroupModel *group = self.dataSource.firstObject;
        (NSMutableArray *)group.cellModels;
    });
    for (id model in self.response.data)
    {
        [arrayM addObject:({
            RBCellModel *cellModel = RBCellModel.new;
            cellModel.reuseCellIdentifier = NSStringFromClass(RBGetCouponListCell.class);
            cellModel.itemSize = CGSizeMake(Zoom(350), Zoom(100));
            cellModel.cellData = model;
            cellModel.eventBlock1 = ^(NSInteger type, NSInteger index, id data) {
                [weak_self createOrderWithModel:data];
            };
            cellModel;
        })];
    }
    [self.collectionView reloadData];
    if(!arrayM.count)
    {
        [self.listView showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
    }
}


#pragma mark - Network

- (NSString *)listPageKey
{
    return @"page";
}
- (NSDictionary *)listParams
{
    return @{
        @"class":self.id,
    };
}
- (NSString *)listRequestPath
{
    return @"hu_coupon";
}
- (NSInteger)ListCurrentPage
{
    return self.response.currentPage;
}
- (BOOL)listLog
{
    return YES;
}
- (BOOL)listPost
{
    return YES;
}
- (Class)listResponseClass
{
    return RBGetCouponResponse.class;
}
- (UIScrollView *)listView
{
    return self.collectionView;
}

- (void)getFirstPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadFirstPageWithDesView:self.listView showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:^(id data) {
        
    } reloadBlock:^{
        [weak_self getFirstPageData];
    }];
}

- (void)getNextPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadNextPageWithSucBlock:^(id data) {
        weak_self.response = data;
        [weak_self appendData];
    } failBlock:^(id data) {
        
    }];
}

- (void)createOrderWithModel: (RBGetCouponModel *)model
{
    __weak typeof(self) weak_self = self;
    if ([model.qudao_type isEqualToString:@"1"]) {
        [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
            dicM[@"coupon_id"] = model.coupon_id;
        } path:@"hu_coupon_receive" post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBResponse *data) {
            if (data.succeed) {
                [weak_self.view showTip:@"领取成功" completion:nil];
                [weak_self getFirstPageData];
            } else {
                [weak_self.view showTip:data.message completion:nil];
            }
        } failBlock:^(RBResponse *data) {
            [weak_self.view showTip:data.message completion:nil];
        } reloadBlock:nil];
        
    } else if ([model.qudao_type isEqualToString:@"2"]) {
        
        [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
            dicM[@"coupon_id"] = model.coupon_id;
        } path:@"hu_coupon_buy" post:YES log:YES responseClass:RBCouponCreateOrderResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBCouponCreateOrderResponse *data) {
            RBCouponPayVC *vc = RBCouponPayVC.new;
            vc.orderNum = data.order_number;
            vc.price = model.price;
            [weak_self.navigationController pushViewController:vc animated:YES];
        } failBlock:^(RBCouponCreateOrderResponse *data) {
            [weak_self.view showTip:data.message completion:nil];
        } reloadBlock:nil];
    }
}

#pragma mark - Event

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

kRBCollectionViewDataSourceAndDelegate(self.dataSource);

#pragma mark - Setter

#pragma mark - Getter

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionView = ({
            UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
            //layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
            UICollectionView *view = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.dataSource = self;
            view.delegate = self;
            view.backgroundColor = UIColor.clearColor;
            view.mj_header = [RBRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
            view.mj_footer = [RBRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(getNextPageData)];
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.top.mas_equalTo(0);
            }];
            view;
        });
    }
    return _collectionView;
}


- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
    }
    return _dataSource;
}

@end
